/**
 * 
 * @authors youzhao.zhou@pandauu.com
 * @date    2016-09-08 15:12:44
 * @version $Id$
 * 
 * 车票兑换页
 * 
 */

;(function(root,factory){

  var dependent = {
    zepto : typeof exports !== 'undefined' ? require('./zepto.js')&&root.$ : root.$,
    underscore : typeof exports !== 'undefined' ? require('../../public_src/js/underscore/underscore.js') : root._,
    Project : typeof exports !== 'undefined' ? require('../../public_src/js/1_self/1_project.js') : root.Project,
    conf : typeof exports !== 'undefined' ? require('../../public_src/js/1_self/2_config.js') : root.conf,
    log : typeof exports !== 'undefined' ? require('../../public_src/js/1_self/3_log.js') : root.p_log,
    path: {
      zepto : '../../p_src/js/convert/zepto.js',
      underscore : '../../public_src/js/underscore/underscore.min',
      Project : '../../public_src/js/1_self/1_project',
      conf : '../../public_src/js/1_self/2_config',
      log : '../../public_src/js/1_self/3_log',
    }
  };
  
  if(typeof exports === 'object' && typeof module === 'object'){

    module.exports = factory(exports,
      dependent.zepto, 
      dependent.underscore,
      dependent.Project, 
      dependent.conf,
      dependent.log
    );

  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.

    define(['exports', 
      dependent.path.zepto, 
      dependent.path.underscore,
      dependent.path.Project, 
      dependent.path.conf,
      dependent.path.log
      ], factory);
  } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
    // CommonJS
    factory(exports, 
      dependent.zepto, 
      dependent.underscore,
      dependent.Project, 
      dependent.conf,
      dependent.log
    );
  } else {
    // Browser globals
    if (!(dependent.Project||dependent.conf||dependent.log)) {
      throw('请加载相应的文件！');
    }
    factory({},
      dependent.zepto, 
      dependent.underscore,
      dependent.Project, 
      dependent.conf,
      dependent.log
    );
  }

})((typeof window !== 'undefined' ? window : this) ,function(exports,$ ,_ , Project, conf, log, undefined){
  'use strict';

  setTimeout(function(){

    if (typeof $ === 'undefined') {
      $ = window.$;
    }

    if (typeof _ === 'undefined') {
      _ = window._;
    }

    /*
      调试开关
    */
    log._logToggle(true);

    var urlConf = conf._conf;
    /*
      项目工具类对象
    */
    var project = new Project();

    /*
      设置导航头部
    */
    project.setHTML('.middle', '登录');

    $('#back').css({background: 'transparent'});
    $('#goIndex a').css({color: 'transparent'});
    $('#goIndex a').bind('click', function(event) {
      event.preventDefault();
    });
    $('#back').unbind('click').bind('click', function(event) {
      /* Act on the event */
      event.preventDefault();

    });


    $('#loginForm .input-group input').unbind('input').bind('input', function(event) {
      /* Act on the event */

      if (!$(this).val()) {
        $(this).removeClass('has-input');
      }else{
        $(this).addClass('has-input');
      }

    });


    $('.jsLogin').on('click', function(event) {
      event.preventDefault();
      var target = $(event.target);
      var eventType = target.data('event');

      switch(eventType){
        case 'showPw':

          if (target.hasClass('pw-right-label')) {
            $(target.data('target')).attr({
              type : 'password'
            });
            target.removeClass('pw-right-label');
          }else{
            $(target.data('target')).attr({
              type : 'text'
            });
            target.addClass('pw-right-label');
          }
            
          break;

        case 'login':
        log._log(target);
          event.preventDefault();
          sendHttp();
          break;
        case 'bind':
          sendHttp();

          break;
      }


      function sendHttp(){
        if($('#loginForm').find('.has-input').length !==  $('[data-use="1"]').length){
          $('#fail').removeClass('s-hide');
          $('.js-warn-content').html('请填写用户名或密码！');
          addCloseEvent('.js-fail');

          return false;
        }

        $(target.data('target')).val(target.data('type'));
        loginAjax($('#loginForm').serialize(),target.data('type'));
      }

    });


    /*
      登录请求
    */

    function loginAjax(data,type){
      $.ajax({
        url: urlConf.apiRoot + urlConf.apiPre.one + urlConf.api.login,
        type: 'POST',
        dataType: 'json',
        data: data
      })
      .done(function(data) {
        if (data.code === 0) {
          if (type === 0) {
            if (data.type==0) {
              window.location.href = urlConf.root + urlConf.static + 'thread.php';
            }else if(data.type==1){
              window.location.href = urlConf.root + urlConf.static + 'bind.php';
            }
          }else{
            window.location.href = urlConf.root + urlConf.static + 'bind.php';
          }
        }else if(data.code === 110){
          $('#fail').removeClass('s-hide');
          $('.js-warn-content').html('用户名或密码错误！');
          addCloseEvent('.js-fail');
        }
        
      })
      .fail(function() {
        log._log("error");
      })
      .always(function() {
        log._log("complete");
      });
    }

    /*
      启动弹框关闭按钮
    */
    function addCloseEvent(selector){
      $(selector).unbind('click').bind('click', function(event) {
        /* Act on the event */
        $($(this).data('target')).addClass('s-hide');

        $(selector).unbind('click');
      });
    }

    window.addEventListener('unload',function(){
      log = null;
    },false);

  },0);

});
